package org.ling.xuan.util;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManager;
import org.ling.xuan.dao.ProjectDao;
import org.ling.xuan.entity.mo.ProjectSetting;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class ReCompilerUtil {
    private static boolean openProjectByFiles(List<String> files) {
        List<ProjectSetting> projects = ProjectDao.listAll();
        List<ProjectSetting> needActive = new ArrayList<>();
        for (ProjectSetting projectSetting : projects) {
            for (String file : files) {
                if (file.endsWith(".java") && file.startsWith(projectSetting.getLocalPre())) {
                    needActive.add(projectSetting);
                }
            }
        }
        List<String> needOpen = new ArrayList<>();
        Project[] prs = ProjectManager.getInstance().getOpenProjects();
        for (ProjectSetting ps : needActive) {
            boolean hasOpen = false;
            for (Project pr : prs) {
                if (LxStrUtil.unionPath(pr.getBasePath()).equals(ps.getProjectPath())) {
                    hasOpen = true;
                    break;
                }
            }
            if (!hasOpen) {
                needOpen.add(ps.getProjectPath());
            }
        }
        for (int i = 0; i < needOpen.size(); i++) {
            try {
                ProjectManager.getInstance().loadAndOpenProject(needOpen.get(i));
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
        return true;
    }

    public boolean reCompilerFiles(List<String> files) {
        boolean f = openProjectByFiles(files);
        if (f) {
            List<ProjectSetting> projects = ProjectDao.listAll();
            Map<String, Set<String>> map = new HashMap<>();
            for (String s : files) {
                for (ProjectSetting pr : projects) {
                    if (s.startsWith(pr.getProjectPath())) {
                        Set<String> ss = map.getOrDefault(pr.getProjectPath(), new HashSet<>());
                        ss.add(s);
                        map.put(pr.getProjectPath(), ss);
                    }
                }
            }
            Project[] prs = ProjectManager.getInstance().getOpenProjects();

        }
        //todo 编译文件
        return true;
    }


    public void reBuildFiles(List<String> files) {
        //todo 将文件迁移至部署目录
    }

    public void rePackFiles(File root, List<String> files) {
        //todo 打包最新编译处理的文件
    }

    public boolean reCompilerAndBuild(List<String> files) {
        if (reCompilerFiles(files)) {
            reBuildFiles(files);
            return true;
        }
        return false;
    }

    public boolean reCompilerAndPack(File root, List<String> files) {
        if (reCompilerFiles(files)) {
            rePackFiles(root, files);
            return true;
        }
        return false;
    }
}
